const Base = require('./base.js');
const {
  think
} = require('thinkjs');
const moment = require('moment');

module.exports = class extends Base {
  async listAction() {
    const page = this.get('page') || 1;
    const size = this.get('size') || 20;
    const model = this.model('figure_apply');

    const query = this.get()
    delete query.page
    delete query.size

    // if (query.name) {
    //   query.name = ['like', `%${query.name}%`]
    // }

    const data = await model.where(query).order(['id DESC']).page(page, size).countSelect();
    return this.success(data);
  }
  async authPayLogAction() {
    const page = this.get('page') || 1;
    const size = this.get('size') || 20;
    const model = this.model('pay_log');
    const data = await model.alias('a').join({
      table: 'user',
      join: 'left',
      as: 'd',
      on: ['user_id', 'id']
    }).field(['a.out_trade_no', 'd.nickname', 'd.name', 'd.mobile', 'd.is_vip', 'a.type', 'a.total', 'a.time', 'a.level', 'a.pay_status', 'a.create_time']).order(['a.id DESC']).page(page, size).countSelect();
    return this.success(data);
  }

  async auditApplyAction() {

    const id = this.get('id');
    const status = this.get('status');
    const model = this.model('figure_apply');

    await model.where({
      id: id
    }).update({
      status: status,
      pay_status: 0
    });

    const data = await model.where({
      id: id
    }).find()

    if(data.figure_id){
      const newData = {
        is_auth: status==3?1:2,
        user_id: data.user_id,
        show: 1,
        phone: data.auth_phone,
        mail: data.auth_mail,
        wechat: data.auth_wechat,
      }
      delete data.create_time
      delete data.sort
      delete data.dated_time
      delete data.show
      delete data.is_vip
      delete data.is_dated
      
      for (const key in data) {
        if (data[key] && key !== 'id' && key!=='user_id' && key!=='name') {
          newData[key] = data[key]
        }
      }

      await this.model('figure').where({id: data.figure_id}).update(newData)
      
    }else{
      delete data.id
      
      const newFigure = await this.model('figure').add(Object.assign(data, {
        user_id: data.user_id, 
        show:1,
        phone: data.auth_phone,
        mail: data.auth_mail,
        wechat: data.auth_wechat,
      }));
      await this.model('figure_apply').where({user_id: data.user_id}).update({
        figure_id: newFigure
      })
    }

    await this.model('user').where({id: data.user_id}).update({
      is_auth: status==3?1:2
    })


    //vip加七天免费时间
    let newDateVip = moment().add(7, 'd').format()
    await this.model('user').where({id: data.user_id}).update({
      end_time: newDateVip,
      is_vip: status==3?1:2,
      name: data.name,
      company: data.company_name || '暂无',
      position: data.position || '暂无',
      weixin: data.wechat
    });
    //vip加时间结束

    return this.success(data);
  }

  async auditApplyVipAction(){
    const id = this.get('id');
    const status = this.get('status');
    const time = this.get('time');
    const model = this.model('figure_apply');

    const detail = await model.where({ id: id }).find()

    if(detail.figure_id){
      await this.model('figure_apply').where({user_id: detail.user_id}).update({
        pay_status: status==1?3:4
      })
      await this.model('figure').where({id: detail.figure_id}).update({
        user_id: detail.user_id,
        is_vip: status
      });

    }else{
      delete detail.id
      const newFigure = await this.model('figure').add(detail);
      await this.model('figure_apply').where({user_id: detail.user_id}).update({
        pay_status: status==1?3:4,
        figure_id: newFigure
      })
    }

    let newDateVip = null
    const user = await this.model('user').where({id: detail.user_id}).find()
    if(moment(user.end_time).isBefore(moment())){
      newDateVip = moment().add(time, 'M').format()
    }else{
      newDateVip = moment(user.end_time).add(time, 'M').format()
    }

    await this.model('user').where({id: detail.user_id}).update({
      end_time: newDateVip,
      is_vip: status,
      name: detail.name,
      company: detail.company_name || '暂无',
      position: detail.position || '暂无',
      weixin: detail.wechat
    });

    return this.success();

  }

  async cancelAuditAuthAction(){
    const figureId = this.get('figureId');
    const userId = this.get('userId');

    await this.model('figure_apply').where({
      user_id: userId
    }).update({
      status: 0
    });

    await this.model('user').where({
      id: userId
    }).update({
      is_vip: 0,
      is_auth: 0,
      end_time: null,
      name: '-',
      company: '-',
      position: '-'
    });
    if(figureId){
      await this.model('figure').where({
        id: figureId
      }).update({
        is_vip: 0,
        is_auth: 0,
        dated_time: null
      });
    }
    return this.success();
  }
}
